﻿using Component.Tools;
using Setme.Domain.Repository;
using Setme.Domain.Repository.Impl;
using Setme.Infrastructure;
using System;
using System.Linq;

namespace Setme.Domain.Service.Impl
{
    public class EmployeeService : IEmployeeService
    {
        //private readonly IUnitOfWork unitofwork;
        //public EmployeeService(IUnitOfWork _unitofwork)
        //{
        //    unitofwork = _unitofwork;
        //}

        public OperationResult CreateEmployee(Model.Employee model)
        {
            using (IUnitOfWork unitofwork = new EntityFrameworkRepositoryContext())
            {
                IEmployeeRepository employeeRepository = new EmployeeRepository(unitofwork as IRepositoryContext);
                if (model.UserName != employeeRepository.GetEntity.FirstOrDefault(m => m.UserName == model.UserName).UserName)
                {
                    employeeRepository.Insert(model);
                    unitofwork.Commit();
                    return new OperationResult(OperationResultType.Success, "用户注册成功.");
                }
                return new OperationResult(OperationResultType.NoChanged, "用户名已存在.");
            }
        }

        public OperationResult ChangeEmployee(Model.Employee model)
        {
            throw new NotImplementedException();
        }
    }
}
